
package com.smartwork.common.verify;

import com.smartwork.common.identifyCode.IdentifyCode;
import com.smartwork.common.utils.CacheUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 向浏览器输出验证码
 * 
 * @author summer
 * @version 1.0
 * 
 */
public class VerifyCodeServlet extends HttpServlet {

    public static final String VERIFY_CODE = "verifyCode";
    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -2197078633186877733L;

    /**
     * Constructor of the object.
     */
    public VerifyCodeServlet() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置浏览器不缓存本页
        response.setHeader("Cache-Control", "no-cache");

        // 生成验证码，写入用户session
        String verifyCode = VerifyCode.generateTextCode(VerifyCode.TYPE_NUM_UPPER, 4, "0oOilJI1");
        request.getSession().setAttribute(VerifyCode.SESSION_KEY, verifyCode);
        // 输出验证码给客户端
        response.setContentType("image/jpeg");
        /*
         * textCode 文本验证码 width 图片宽度 height 图片高度 interLine 图片中干扰线的条数
         * randomLocation 每个字符的高低位置是否随机 backColor 图片颜色，若为null，则采用随机颜色 foreColor
         * 字体颜色，若为null，则采用随机颜色 lineColor 干扰线颜色，若为null，则采用随机颜色
         */
        BufferedImage bim = VerifyCode.generateImageCode(verifyCode, 80, 25, 12, true, Color.WHITE, Color.RED, null);
        ImageIO.write(bim, "JPEG", response.getOutputStream());

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置不缓存图片
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0) ;
        //指定生成的相应图片
        response.setContentType("image/jpeg") ;
        IdentifyingCode idCode = new IdentifyingCode();
        BufferedImage image =new BufferedImage(idCode.getWidth() , idCode.getHeight() , BufferedImage.TYPE_INT_BGR) ;
        Graphics2D g = image.createGraphics() ;
        //定义字体样式
        Font myFont = new Font("黑体" , Font.BOLD , 16) ;
        //设置字体
        g.setFont(myFont) ;

        g.setColor(idCode.getRandomColor(200 , 250)) ;
        //绘制背景
        g.fillRect(0, 0, idCode.getWidth() , idCode.getHeight()) ;

        g.setColor(idCode.getRandomColor(180, 200)) ;
        idCode.drawRandomLines(g, 160) ;
        // 生成验证码，写入用户session
        String verifyCode = idCode.drawRandomString(4, g) ;
        System.out.println("验证码:"+verifyCode);
        String mobile=(String)request.getParameter("mobile");
        CacheUtils.put(VERIFY_CODE+mobile,verifyCode);
        g.dispose() ;
        ImageIO.write(image, "JPEG", response.getOutputStream()) ;
    }
}
